package com.cusc.module_monitor_location.mvvm.vm;

import android.app.Application;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableBoolean;
import androidx.databinding.ObservableField;
import androidx.drawerlayout.widget.DrawerLayout;

import com.cusc.lib_api.bean.RespDTO;
import com.cusc.lib_api.bean.department.NodeDepartment;
import com.cusc.lib_api.bean.department.request.Req_department;
import com.cusc.module_monitor_location.R;
import com.cusc.module_monitor_location.entry.NodeBean;
import com.cusc.module_monitor_location.mvvm.model.LocationMonitorRegister;
import com.cusc.module_monitor_location.utils.DepartmentUtils;
import com.tcgroup.phone.base_core.event.event_livedata.SingleLiveEvent;
import com.tcgroup.phone.base_core.framework.mvvm.viewmodel.BaseViewModel;
import com.tcgroup.phone.base_core.utils.ToastUtils;
import com.tcgroup.phone.base_core.widget.TextImageButton;

import java.util.ArrayList;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 作者: wangfei
 * 创建时期: 2020/4/3.
 * 包名: com.cusc.module_monitor_location.mvvm.vm
 * 类名: OSAndroidMvvm
 */
public class LocationMonitorViewModel extends BaseViewModel<LocationMonitorRegister> implements
        DrawerLayout.DrawerListener, TextImageButton.OnItemClickListener {

    public ObservableField<Integer> drawerLockMode = new ObservableField<>(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    public ObservableBoolean tibMotionImage = new ObservableBoolean(false);
    public ObservableBoolean tibStaticImage = new ObservableBoolean(false);
    public ObservableBoolean tibOfflineImage = new ObservableBoolean(false);
    public ObservableBoolean tibOnlineImage = new ObservableBoolean(false);
    protected SingleLiveEvent<ArrayList<NodeBean>> nodeBeanListViewEvent;
    protected SingleLiveEvent<ArrayList<NodeBean>> childNodeBeanListViewEvent;

    public LocationMonitorViewModel(@NonNull Application application, LocationMonitorRegister netRegister) {
        super(application, netRegister);
    }

    public SingleLiveEvent<ArrayList<NodeBean>> getNodeBeanListViewEvent(){
        return nodeBeanListViewEvent = createLiveData(nodeBeanListViewEvent);
    }

    public SingleLiveEvent<ArrayList<NodeBean>> getChildNodeBeanListViewEvent(){
        return childNodeBeanListViewEvent = createLiveData(childNodeBeanListViewEvent);
    }

    @Override
    public void initViewByReFresh() {
        super.initViewByReFresh();
        getDepartmentToBus(null);
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {

    }

    @Override
    public void onDrawerOpened(@NonNull View drawerView) {

    }

    @Override
    public void onDrawerClosed(@NonNull View drawerView) {

    }

    @Override
    public void onDrawerStateChanged(int newState) {

    }

    /**
     * 获取部门车辆信息数据
     *
     * @param id
     * @param
     */
    public void getDepartmentToBus(String id) {
        Req_department department;
        if (TextUtils.isEmpty(id)) {
            department = new Req_department("1", "1", "1");
        } else {
            department = new Req_department(id, "1", "1", "1");
        }
        postShowInitLoadViewEvent(true);
        mNetRegister.getDepartmentToBus(department).subscribe(new Observer<RespDTO<ArrayList<NodeDepartment>>>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(RespDTO<ArrayList<NodeDepartment>> nodeDepartmentRespDTO) {
                postShowCurrentEvent(true);
                ArrayList<NodeDepartment> departmentArrayList = nodeDepartmentRespDTO.detail;
                if (null == departmentArrayList || departmentArrayList.size() <= 0) {
                    return;
                }
                if (TextUtils.isEmpty(id)){
                    getNodeBeanListViewEvent().setValue(DepartmentUtils.ConvertNodeBean(departmentArrayList));
                }else{
                    getChildNodeBeanListViewEvent().setValue(DepartmentUtils.ConvertNodeBean(departmentArrayList));
                }
            }

            @Override
            public void onError(Throwable e) {
                postShowCurrentEvent(true);
            }

            @Override
            public void onComplete() {
                postShowCurrentEvent(true);
            }
        });
    }

    @Override
    public void setOnItemClick(int id, boolean flag) {
        if (id == R.id.tib_motion) {
            tibMotionImage.set(flag);
            ToastUtils.showShort("哈哈哈哈");
            return;
        }
        if (id == R.id.tib_static) {
            tibStaticImage.set(flag);
            return;
        }
        if (id == R.id.tib_offline) {
            tibOfflineImage.set(flag);
            return;
        }
        if (id == R.id.tib_online) {
            tibOnlineImage.set(flag);
            return;
        }
    }
}
